package cn.iocoder.yudao.module.pypm.service.iteration;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pypm.controller.admin.iteration.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.iteration.IterationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pypm.dal.mysql.iteration.IterationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pypm.enums.ErrorCodeConstants.*;

/**
 * 迭代信息 Service 实现类
 *
 * @author 钦州码头
 */
@Service
@Validated
public class IterationServiceImpl implements IterationService {

    @Resource
    private IterationMapper iterationMapper;

    @Override
    public Long createIteration(IterationSaveReqVO createReqVO) {
        // 插入
        IterationDO iteration = BeanUtils.toBean(createReqVO, IterationDO.class);
        iterationMapper.insert(iteration);
        // 返回
        return iteration.getId();
    }

    @Override
    public void updateIteration(IterationSaveReqVO updateReqVO) {
        // 校验存在
        validateIterationExists(updateReqVO.getId());
        // 更新
        IterationDO updateObj = BeanUtils.toBean(updateReqVO, IterationDO.class);
        iterationMapper.updateById(updateObj);
    }

    @Override
    public void deleteIteration(Long id) {
        // 校验存在
        validateIterationExists(id);
        // 删除
        iterationMapper.deleteById(id);
    }

    private void validateIterationExists(Long id) {
        if (iterationMapper.selectById(id) == null) {
            throw exception(ITERATION_NOT_EXISTS);
        }
    }

    @Override
    public IterationDO getIteration(Long id) {
        return iterationMapper.selectById(id);
    }

    @Override
    public PageResult<IterationDO> getIterationPage(IterationPageReqVO pageReqVO) {
        return iterationMapper.selectPage(pageReqVO);
    }

}